<?php

defined('_JEXEC') or die('Restricted access');
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');

class ConsoleModelCampaigntracking  extends JModelList
{
    protected function getListQuery()
    {
        $app = JFactory::getApplication('site');
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $user = JFactory::getUser();
        $params = $app->getParams();
        $this->setState('params', $params);
        $query->select(
            'campaign.*, app.`name` as app_name'
        )
            ->from('pp_campaign_cfg campaign')
//            ->join('INNER', 'pp_channel_cfg channel on channel.id=campaign.channel_id')
            ->join('INNER', 'pp_app app on app.id=campaign.app_id')
            ->where('app.account_id='.$user->id);
        $query->order('campaign.create_date desc');
        return $query;
    }

    public function events($appid = null) {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')->from('pp_event_cfg')->where('app_id=' . $appid)->order('id');
        $db->setQuery($query);
        $items = $db->loadObjectList();

        return $items;
    }

    public function store($inputs)
    {
        $trackDate = JFactory::getDate()->format('Y-m-d');
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->insert('pp_campaign_cfg')
            ->columns(
                array(
                    $db->quoteName('app_id'),
                    $db->quoteName('name'),$db->quoteName('lookback_window'),
                    $db->quoteName('open_id'),
                    //$db->quoteName('cost_src'),  $db->quoteName('cost'),
                    $db->quoteName('create_date')
                )
            )
            ->values('  "'.$inputs['app_id'].'", "'.$inputs['name'].'", "'.$inputs['lookback_window'].'", "'.$inputs['open_id'].'","'.$trackDate.'" ' );

        $db->setQuery($query);

        try
        {
            $insertId = $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
       /* $insertId = $db->insertid();
        if ($insertId && $inputs['map']) {
            $this->syncMap($insertId,$inputs['map']);
        }*/
        return true;
    }

    /**sync   campaign_event_mapping
     * @param $campaign_id
     * @param $tag
     */
    public function syncMap($campaign_id, $tag)
    {
        $count = count($tag['event_id']);
        for ($i=0; $i<$count; $i++) {
            $data['campaign_id'] = $campaign_id;
            $data['event_id'] = $tag['event_id'][$i];
            $data['partner_event_id'] = $tag['event_tag'][$i];
            $this->storeMapping($data);
        }
        return true;
    }

    public function storeMapping($inputs)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->insert('pp_campaign_event_mapping')
            ->columns(
                array(
                    $db->quoteName('campaign_id'),
                    $db->quoteName('event_id'), $db->quoteName('partner_event_id')
                )
            )
            ->values(' "'.$inputs['campaign_id'].'",  "'.$inputs['event_id'].'", "'.$inputs['partner_event_id'].'" ');

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        return true;
    }

    public function getItem($id = null)
    {

        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')
            ->from('pp_campaign_cfg')->where('id='.$id);
        $db->setQuery($query);
        $Item= $db->loadObject();
        return $Item;
    }

    public function getCampaignChannel($campaign_id)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('campaign.*,channel.name')->from('pp_campaign_channel_cfg campaign')
            ->join('INNER', 'pp_channel_cfg channel on channel.id=campaign.channel_id')
            ->where('campaign.campaign_id=' . $campaign_id)->order('campaign.id');
        $db->setQuery($query);
        $items = $db->loadObjectList();
        return $items;
    }
}